<!--
 * @Descripttion: 熊大写的
 * @version: 1.0.0
 * @Author: xt_xiong
 * @Date: 2022-01-18 15:28:08
 * @LastEditors: xt_xiong
 * @LastEditTime: 2022-01-21 20:26:13
-->
<template>
  <div class="container">
    <el-card>
      <div class="left">
        <!-- 表头区域 -->
        <el-form inline ref="form" :model="form" label-width="80px">
          <el-form-item label="学科名称">
            <el-input v-model="form.name"> </el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear">清除</el-button>
            <el-button type="primary" @click="search">搜索</el-button>
          </el-form-item>
          <el-form-item style="float: right">
            <el-button type="success" @click="dialogVisible = true"
              >新增学科</el-button
            >
          </el-form-item>
          <el-form-item style="display: block; background-color: #f4f4f5">
           <el-alert
           :closable="false"
           :title="`数据一共${this.total}条`"
           type="info"
           show-icon
           >
           </el-alert>
          </el-form-item>
        </el-form>
        <!-- 表单区域 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column type="index" :index="indexMethods" label="序号">
          </el-table-column>
          <el-table-column
            width="120"
            sortable
            prop="subjectName"
            label="学科名称"
          >
          </el-table-column>
          <el-table-column sortable width="120" prop="username" label="创建者">
          </el-table-column>
          <el-table-column
            sortable
            prop="addDate"
            :formatter="dateFormat"
            width="180"
            label="创建日期"
          >
            <!-- <template slot-scope="scope">
        {{scope.row.addDate|formatDate('hh:mm:ss')}}
    </template> -->
          </el-table-column>
          <el-table-column
            sortable
            prop="isFrontDisplay"
            width="120"
            label="前台是否显示"
          >
          </el-table-column>
          <el-table-column
            sortable
            prop="twoLevelDirectory"
            width="120"
            label="二级目录"
          >
          </el-table-column>
          <el-table-column sortable prop="tags" width="120" label="标签">
          </el-table-column>
          <el-table-column sortable prop="totals" width="120" label="题目数量">
          </el-table-column>
          <el-table-column fixed="right" prop="name" label="操纵">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="
                  $router.push({
                    path: '/subjects/directorys',
                    query: { id: scope.row.id, name: scope.row.subjectName }
                  })
                "
                size="small"
                >学科分类
              </el-button>
              <el-button
                type="text"
                @click="
                  $router.push({
                    path: '/subjects/tags',
                    query: { id: scope.row.id, name: scope.row.subjectName }
                  })
                "
                size="small"
                >学科标签
              </el-button>
              <el-button
                type="text"
                @click.native="editSubMes(scope.row)"
                size="small"
                >修改
              </el-button>
              <el-button
                type="text"
                @click.native="delSub(scope.row.id)"
                size="small"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination
          style="float: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="subQuery.page"
          :page-sizes="[10, 20, 50]"
          :page-size="subQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
        <!-- dialog区域 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
          <!-- :before-close="handleClose" -->
          <subjectDialog
            ref="subData"
            v-if="dialogVisible"
            :row="row"
            :is-edit="isEdit"
            :visible.sync="dialogVisible"
            @close="closeHandler"
          >
          </subjectDialog>
        </el-dialog>
      </div>
      <div class="right"></div>
    </el-card>
  </div>
</template>

<script>
import moment from 'moment'
import { list, remove } from '@/api/hmmm/subjects.js'
import subjectDialog from './subjectDialog.vue'
export default {
  name: '',
  components: {
    subjectDialog
  },
  data () {
    return {
      row: {},
      total: 1,
      dialogVisible: false,
      subjectDialog: false,
      isEdit: false,
      subQuery: {
        page: 1,
        pagesize: 10
      },
      form: {
        name: ''
      },
      tableData: []
    }
  },
  created () {
    this.getSubMes()
  },
  methods: {
    // 删除学科
    async delSub (data) {
      this.$confirm('此操作将删除学科 ' + ', 继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: data })
            .then((response) => {
              this.$message.success('成功删除了学科' + '!')
              this.tableData.splice(data, 1)
              this.getSubMes(this.subQuery)
            })
            .catch((response) => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    // search查询
    async search () {
      this.subQuery.subjectName = this.form.name
      const res = await this.getSubMes()
      console.log(res)
    },
    // 修改学科分类
    editSubMes (row) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.subData.Subject = { ...row }
      })
      this.isEdit = true
    },
    // moment格式化时间
    dateFormat (row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },

    // 发ajax 渲染表格 函数
    async getSubMes () {
      const { data: res } = await list(this.subQuery)
      this.tableData = res.items
      this.total = res.counts
    },
    // 清除search内容
    clear () {
      this.form.name = ''
    },
    // 关闭对话框
    closeHandler () {
      this.dialogVisible = false
      this.isEdit = false
      // window.Location.reload()
      this.getSubMes()
    },
    // 序号排序方法
    indexMethods (index) {
      if (this.page !== 1) {
        return index + 1 + (this.subQuery.page - 1) * this.subQuery.pagesize
      }
      return index + 1
    },
    // 切换每页数量
    handleSizeChange (val) {
      this.subQuery.pagesize = val
      console.log(`每页 ${val} 条`)
      this.getSubMes()
    },
    // 点击数量进行跳转
    handleCurrentChange (val) {
      this.subQuery.page = val
      console.log(`当前页: ${val}`)
      this.getSubMes()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px 30px;
}
.page-tools {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .tips {
    line-height: 34px;
    padding: 0px 15px;
    border-radius: 5px;
    border: 1px solid rgba(145, 213, 255, 1);
    background: rgba(230, 247, 255, 1);
    i {
      margin-right: 10px;
      color: #409eff;
    }
  }
}
</style>
